package com.megoc.menu.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v4.view.ViewPager;
import android.text.TextPaint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.megoc.menu.R;
import com.megoc.menu.model.BaseList;
import com.megoc.menu.model.collect.ListRecommed;
import com.megoc.menu.model.collect.Result;
import com.megoc.menu.view.CircleIndicator;
import com.megoc.menu.view.MenuGridView;
import com.megoc.menu.view.MenuListView;

import java.util.List;

/**
 * Created by Megoc on 2015/10/12.
 */
public class MenuListAdapter extends BaseAdapter {
    private Result result;
    private LayoutInflater inflater;
    private Context context;
    private int i=0;
    public MenuListAdapter(Result result, Context context) {
        this.result = result;
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 9;
    }

    @Override
    public int getCount() {
        return 9;
    }

    @Override
    public Object getItem(int position) {
        return result;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        RecipeHolder recipeHolder = null;
        ToolsHolder toolsHolder = null;
        ToolsHolder viptoolsHolder = null;
        AlbumHolder albumHolder = null;
        PersonHoder personHoder = null;
        EventHolder eventHolder = null;
        EventHolder readeventHolder=null;
        RecommedHolder recommedHolder=null;
        AdHolder adHolder=null;
        int type = getItemViewType(position);
        if (view == null) {
            Log.d("getview的次数",""+i);
            i++;
            switch (type) {
                case 0:
                    List<BaseList> lists = result.getRecipe().getList();
                    view = inflater.inflate(R.layout.menu_recipe, null);
                    recipeHolder = new RecipeHolder();
                    recipeHolder.mViewpager = (ViewPager) view.findViewById(R.id.viewpager);
                    recipeHolder.mCircleIndicator = (CircleIndicator) view.findViewById(R.id.indicator);
                    recipeHolder.mMenuHeaderPagerAdapter = new MenuHeaderPagerAdapter(lists, context);
                    view.setTag(recipeHolder);
                    break;
                case 1:
                    view = inflater.inflate(R.layout.menu_tools, null);
                    toolsHolder = new ToolsHolder();
                    toolsHolder.mGridView = (MenuGridView) view.findViewById(R.id.menu_tools_gridview);
                    toolsHolder.mMenuVipGridViewAdapter = new MenuVipGridViewAdapter(result.getTools().getList(), context);
                    view.setTag(toolsHolder);

                    break;
                case 2:

                    view = inflater.inflate(R.layout.menu_album, null);
                    albumHolder = new AlbumHolder();
                    albumHolder.mGridView = (MenuGridView) view.findViewById(R.id.menu_album_gridview);
                    albumHolder.mMenuAlbumGridViewAdapter = new MenuAlbumGridViewAdapter(result.getAlbum().getList(), context);
                    view.setTag(albumHolder);

                    break;
                case 3:
                    view = inflater.inflate(R.layout.menu_person, null);
                    personHoder=new PersonHoder();
                    personHoder.mMenuGridView = (MenuGridView) view.findViewById(R.id.person_gridview);
                    personHoder.mMenuGridViewAdapter = new MenuGridViewAdapter(result.getPerson().getTag(), context);

                    personHoder.mGridView = (MenuGridView) view.findViewById(R.id.menu_person_gridview);
                    personHoder.mMenuPersonGridViewAdapter = new MenuPersonGridViewAdapter(result.getPerson().getList(), context);
                    view.setTag(personHoder);
                    break;
                case 4:
                    view = inflater.inflate(R.layout.menu_recommed, null);
                    recommedHolder=new RecommedHolder();
                    List<ListRecommed> listRecommed = result.getRecommed().getList();

                    recommedHolder.leftSimpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.menu_recommed_left_img);
                    recommedHolder.leftTitileView = (TextView) view.findViewById(R.id.menu_recommed_left_text_title);
                    recommedHolder.leftDitailView = (TextView) view.findViewById(R.id.menu_recommed_left_text_detail);

                    recommedHolder.lefturi = Uri.parse(listRecommed.get(0).getImg());


                    TextPaint tp = recommedHolder.leftTitileView.getPaint();
                    tp.setFakeBoldText(true);

                    recommedHolder.rightSimpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.menu_recommed_right_img);
                    recommedHolder.rightTitileView = (TextView) view.findViewById(R.id.menu_recommed_right_text_title);
                    recommedHolder.rightDitailView = (TextView) view.findViewById(R.id.menu_recommed_right_text_detail);

                    recommedHolder.righturi = Uri.parse(listRecommed.get(1).getImg());

                    TextPaint tp2 = recommedHolder.rightTitileView.getPaint();
                    tp2.setFakeBoldText(true);
                    view.setTag(recommedHolder);
                    break;
                case 5:
                    view = inflater.inflate(R.layout.menu_ad, null);
                    adHolder=new AdHolder();
                    adHolder.mSimpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.menu_ad_img);
                    adHolder.uri=Uri.parse(result.getAd().getImg());
                    view.setTag(adHolder);
                    break;
                case 6:
                    view = inflater.inflate(R.layout.menu_read, null);
                    readeventHolder=new EventHolder();
                    readeventHolder.listViewEvent = (MenuListView) view.findViewById(R.id.menu_read_listview);
                    readeventHolder.mMenuReadListAdapterEvent = new MenuReadListAdapter(result.getRead().getList(), context);
                    view.setTag(readeventHolder);
                    break;
                case 7:
                    view = inflater.inflate(R.layout.menu_event, null);
                    eventHolder=new EventHolder();
                    eventHolder.listViewEvent = (MenuListView) view.findViewById(R.id.menu_event_listview);
                    eventHolder.mMenuReadListAdapterEvent = new MenuReadListAdapter(result.getEvent().getList(), context);
                    view.setTag(eventHolder);
                    break;
                case 8:
                    view = inflater.inflate(R.layout.menu_vip, null);
                    viptoolsHolder = new ToolsHolder();
                    viptoolsHolder.mGridView = (MenuGridView) view.findViewById(R.id.menu_vip_gridview);
                    viptoolsHolder.mMenuVipGridViewAdapter = new MenuVipGridViewAdapter(result.getVip().getList(), context);
                    view.setTag(viptoolsHolder);
                    break;
            }

        } else {
            switch (type) {
                case 0:
                    recipeHolder = (RecipeHolder) view.getTag();
                    break;
                case 1:
                    toolsHolder = (ToolsHolder) view.getTag();
                    break;
                case 2:
                    albumHolder = (AlbumHolder) view.getTag();
                    break;
                case 3:
                    personHoder= (PersonHoder) view.getTag();
                    break;
                case 4:
                    recommedHolder= (RecommedHolder) view.getTag();
                    break;
                case 5:
                    adHolder= (AdHolder) view.getTag();
                    break;
                case 6:
                    readeventHolder= (EventHolder) view.getTag();
                    break;
                case 7:
                    eventHolder= (EventHolder) view.getTag();
                    break;
                case 8:
                    viptoolsHolder = (ToolsHolder) view.getTag();
                    break;
            }
        }
        switch (type) {
            case 0:
                recipeHolder.mViewpager.setAdapter(recipeHolder.mMenuHeaderPagerAdapter);
                recipeHolder.mCircleIndicator.setViewPager(recipeHolder.mViewpager);
                break;
            case 1:
                toolsHolder.mGridView.setAdapter(toolsHolder.mMenuVipGridViewAdapter);
                break;
            case 2:
                albumHolder.mGridView.setAdapter(albumHolder.mMenuAlbumGridViewAdapter);
                break;
            case 3:
                personHoder.mMenuGridView.setAdapter(personHoder.mMenuGridViewAdapter);
                personHoder.mGridView.setAdapter(personHoder.mMenuPersonGridViewAdapter);
                break;
            case 4:
                List<ListRecommed> listRecommed = result.getRecommed().getList();
                recommedHolder.leftSimpleDraweeView.setImageURI(recommedHolder.lefturi);
                recommedHolder.leftTitileView.setText(listRecommed.get(0).getTitle());
                recommedHolder.leftDitailView.setText(listRecommed.get(0).getIntro());
                recommedHolder.rightSimpleDraweeView.setImageURI(recommedHolder.righturi);
                recommedHolder.rightTitileView.setText(listRecommed.get(1).getTitle());
                recommedHolder.rightDitailView.setText(listRecommed.get(1).getIntro());
                break;
            case 5:
                adHolder.mSimpleDraweeView.setImageURI(adHolder.uri);
                break;
            case 6:
                readeventHolder.listViewEvent.setAdapter(readeventHolder.mMenuReadListAdapterEvent);
               // Utility.setListViewHeightBasedOnChildren(readeventHolder.listViewEvent);
                break;
            case 7:
                eventHolder.listViewEvent.setAdapter(eventHolder.mMenuReadListAdapterEvent);
              //  Utility.setListViewHeightBasedOnChildren(eventHolder.listViewEvent);
                break;
            case 8:
                viptoolsHolder.mGridView.setAdapter(viptoolsHolder.mMenuVipGridViewAdapter);
                break;
        }
        return view;
    }

    class RecipeHolder {
        ViewPager mViewpager;
        CircleIndicator mCircleIndicator;
        MenuHeaderPagerAdapter mMenuHeaderPagerAdapter;
    }

    class ToolsHolder {
        MenuGridView mGridView;
        MenuVipGridViewAdapter mMenuVipGridViewAdapter;
    }

    class AlbumHolder {
        MenuGridView mGridView;
        MenuAlbumGridViewAdapter mMenuAlbumGridViewAdapter;
    }

    class PersonHoder {
        MenuGridView mMenuGridView;
        MenuGridViewAdapter mMenuGridViewAdapter;

        MenuGridView mGridView;
        MenuPersonGridViewAdapter mMenuPersonGridViewAdapter;
    }
    class EventHolder{
        MenuListView listViewEvent;
        MenuReadListAdapter mMenuReadListAdapterEvent ;
    }
    class AdHolder{
        SimpleDraweeView mSimpleDraweeView;
        Uri uri;
    }
    class RecommedHolder{
        SimpleDraweeView leftSimpleDraweeView;
        TextView leftTitileView;
        TextView leftDitailView;
        Uri lefturi;

        SimpleDraweeView rightSimpleDraweeView;
        TextView rightTitileView;
        TextView rightDitailView;
        Uri righturi;
    }
}
